package api

import (
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	log "github.com/sirupsen/logrus"
	"net/http"
	"user-center/src/service"
)

var upgrader = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool {
		// 在这里进行权限验证，返回true表示验证通过，允许连接
		// 你可以根据需要实现自己的验证逻辑
		return true
	},
}

// CodeUpgrader GET
// @Tags        code
// @Summary     升级成websocket连接
// @Description   升级成websocket连接
// @Router /api/code/ws [get]
func CodeUpgrader(ctx *gin.Context) {
	CodeserveWebSocket(ctx.Writer, ctx.Request)
}

func CodeserveWebSocket(w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Println("code websocket upgrader error: ", err)
		return
	}
	defer conn.Close()
	conn.SetPingHandler(func(appData string) error {
		return nil
	})
	service.CodeDealWebsocketConn(conn)
}

